package com.bdmc.alarm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.alarm.domain.bo.AlarmRuleConfigBo;
import com.bdmc.alarm.domain.vo.AlarmRuleConfigVo;
import com.bdmc.alarm.domain.AlarmRuleConfig;
import com.bdmc.alarm.mapper.AlarmRuleConfigMapper;
import com.bdmc.alarm.service.IAlarmRuleConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 报警规则详细配置Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-06
 */
@Service
public class AlarmRuleConfigServiceImpl extends ServicePlusImpl<AlarmRuleConfigMapper, AlarmRuleConfig, AlarmRuleConfigVo> implements IAlarmRuleConfigService {

    @Override
    public AlarmRuleConfigVo queryById(Integer ruleConfigId){
        return getVoById(ruleConfigId);
    }

    @Override
    public TableDataInfo<AlarmRuleConfigVo> queryPageList(AlarmRuleConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AlarmRuleConfig> lqw = buildQueryWrapper(bo);
        Page<AlarmRuleConfigVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<AlarmRuleConfigVo> mySelectPage(AlarmRuleConfigBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<AlarmRuleConfigVo> queryList(AlarmRuleConfigBo bo) {
        LambdaQueryWrapper<AlarmRuleConfig> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<AlarmRuleConfig> buildQueryWrapper(AlarmRuleConfigBo bo) {
        LambdaQueryWrapper<AlarmRuleConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAlarmRuleId() != null, AlarmRuleConfig::getAlarmRuleId, bo.getAlarmRuleId());
        lqw.like(StringUtils.isNotBlank(bo.getRuleConfigName()), AlarmRuleConfig::getRuleConfigName, bo.getRuleConfigName());
        lqw.eq(bo.getRuleConfigType() != null, AlarmRuleConfig::getRuleConfigType, bo.getRuleConfigType());
        lqw.eq(bo.getEnabled() != null, AlarmRuleConfig::getEnabled, bo.getEnabled());
        return lqw;
    }

    @Override
    public Boolean insertByBo(AlarmRuleConfigBo bo) {
        AlarmRuleConfig add = BeanUtil.toBean(bo, AlarmRuleConfig.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setRuleConfigId(add.getRuleConfigId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(AlarmRuleConfigBo bo) {
        AlarmRuleConfig update = BeanUtil.toBean(bo, AlarmRuleConfig.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(AlarmRuleConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
